package com.my.commonlib.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.view.Gravity;
import android.widget.Toast;

import com.my.commonlib.BaseApplication;

public class Tips {
	// 提示等待信息的对话框
	private static ProgressDialog waittingDialog = null;
	
	// 浮层提示信息
	private static Toast tip_layer = null;

	// 异步任务:一个等待对话框一般会绑定一个异步任务，如果用户按返回键关闭等待对话框，同时也会结束这个异步任务
	public static AsyncTask<String, Integer, String> currentTask = null;

	// 全局警告窗口句柄
	private static AlertDialog mGlobalAlertDialogHandle = null;
	
	//Log输出
	private static String TAG = "FootballCube";

	/**
	 * 显示浮层提示信息
	 * 
	 * @author agger
	 * @date 2014-7-1
	 * @param strResID
	 */
	public static void showTips(Activity activity3, int strResID) {
		showTips(activity3, StrUtil.getString(strResID), Toast.LENGTH_SHORT, null);
	}

	/**
	 * 显示浮层提示信息
	 * 
	 * @author agger
	 * @date 2014-7-1
	 * @param content
	 */
	public static void showTips(Activity activity, String content) {
		showTips(activity, content, Toast.LENGTH_SHORT, null);
	}

	public static void showTips(Activity activity, String content, Activity tipsShowOnActivity) {
		showTips(activity, content, Toast.LENGTH_SHORT, tipsShowOnActivity);
	}

	public static void showLongTips(Activity activity, int strResID) {
		showTips(activity, StrUtil.getString(strResID), Toast.LENGTH_LONG, null);
	}

	public static void showLongTips(Activity activity, String content) {
		showTips(activity, content, Toast.LENGTH_LONG, null);
	}

	/**
	 * 显示浮层提示信息，可传参控制显示的时间长短
	 * 
	 * @author agger
	 * @date 2014-7-1
	 * @param content 要显示的提示信息
	 * @param duration 信息显示的时长 (可以选择 Toast.LENGTH_LONG 或者 Toast.LENGTH_SHORT)
	 */
	public static synchronized void showTips(final Activity activity2, final String content, final int duration, Activity tipsShowOnActivity) {


		final Activity activity = activity2;

		if (null == activity || null == content || content.length() < 1) {
			return;
		}

		if (null != tipsShowOnActivity && !tipsShowOnActivity.equals(activity)) {
			return;
		}

		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (null == tip_layer) {
					tip_layer = Toast.makeText(BaseApplication.getApp(), "", Toast.LENGTH_SHORT);
					tip_layer.setGravity(Gravity.CENTER, 0, 0);
				}

				// 显示帮助或者提示信息的浮层
				tip_layer.setText(content);
				tip_layer.setDuration(duration);
				tip_layer.show();
			}
		});
	}




	/**
	 * 隐藏等待提示信息的对话框，同时结束可能绑定等待对话框的异步任务
	 * 
	 * @author agger
	 * @date 2014-7-1
	 */
	public static synchronized void hiddenWaitingTips() {
		if (waittingDialog != null) {
			waittingDialog.dismiss();
			waittingDialog = null;
			currentTask = null;
		}
	}

	/**
	 * 隐藏当前 Activity 所附属的 waittingDialog
	 * 
	 * @author agger
	 * @date 2014-7-1
	 */
	public static synchronized void hiddenWaitingTips(Activity activity) {
		if (waittingDialog != null) {
			if(activity == waittingDialog.getOwnerActivity()){
				waittingDialog.dismiss();
				waittingDialog = null;
				currentTask = null;
			}
		}
	}

	/**
	 * 结束当前正在执行的异步任务
	 * 
	 * @author agger
	 * @date 2014-7-1
	 */
	public static synchronized void stopCurrentTask() {
		if (currentTask != null) {
			try {
				currentTask.cancel(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
			currentTask = null;
		}
	}

	/**
	 * 判断用Tips类最后一次弹出的对话框是否正在显示
	 * 
	 * @author agger
	 * @date 2014-7-1
	 * @return boolean
	 */
	public static synchronized boolean isAlertShowing() {
		if (null != mGlobalAlertDialogHandle) {
			try {
				return mGlobalAlertDialogHandle.isShowing();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return false;
	}




	
}
